package com.java.tutorials.jdk7.fileattribute;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.PosixFileAttributes;
import java.nio.file.attribute.PosixFilePermission;
import java.nio.file.attribute.PosixFilePermissions;
import java.util.Set;

import static java.nio.file.attribute.PosixFilePermission.*;
/**
 * Created by xuzhike on 2016/1/24.
 * http://www.java2s.com/Code/Java/JDK-7/UsingPosixFileAttributes.htm
 */
public class UsingPosixFileAttributesTest {
    public static void main(String[] args) throws Exception {

        Path profile = Paths.get("/user/Admin/.profile");

        PosixFileAttributes attrs = Files.readAttributes(profile,
                PosixFileAttributes.class);

        Set<PosixFilePermission> posixPermissions = attrs.permissions();
        posixPermissions.clear();

        String owner = attrs.owner().getName();
        String perms = PosixFilePermissions.toString(posixPermissions);
        System.out.format("%s %s%n", owner, perms);

        posixPermissions.add(OWNER_READ);
        posixPermissions.add(GROUP_READ);
        posixPermissions.add(OWNER_READ);
        posixPermissions.add(OWNER_WRITE);
        Files.setPosixFilePermissions(profile, posixPermissions);

    }
}
